package com.xcf.controller.business.finance;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xcf.controller.base.BaseController;
import com.xcf.controller.base.ExportDataHandler;
import com.xcf.entity.Page;
import com.xcf.entity.business.entities.Entity;
import com.xcf.service.business.entities.EntityManager;
import com.xcf.service.business.finance.ReceiptService;
import com.xcf.service.system.appuser.AppuserManager;
import com.xcf.service.system.menu.MenuManager;
import com.xcf.service.system.user.UserManager;
import com.xcf.util.DateUtil;
import com.xcf.util.Jurisdiction;
import com.xcf.util.PageData;
import com.xcf.util.ResponseResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/***
 * 收款管理
 * @author WongBin
 */
@Controller
@RequestMapping(value="/receiptMgr")
public class ReceiptController extends BaseController implements ExportDataHandler{
	
	String menuUrl = "receiptMgr/index"; //菜单地址(权限用)
    final String base = "/business/finance";

	@Resource(name="menuService")
	private MenuManager menuService;
	@Resource(name="entityService")
	private EntityManager entityService;
	@Resource(name="userService")
	private UserManager userService;
	@Resource(name="appuserService")
	private AppuserManager appuserService;
	
	@Resource
	private ReceiptService receiptService;
	
	/**
     *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping("/index")
	public ModelAndView list()throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			if(pd.getString("ENTITY_ID") == null || "".equals(pd.getString("ENTITY_ID").trim())){
				pd.put("ENTITY_ID", "1");										//默认列出第一组角色(初始设计系统用户和会员组不能删除)
			}
			PageData fpd = new PageData();
			fpd.put("ENTITY_ID", "0");
			List<Entity> entityList = entityService.listAllEntitiesByPId(fpd);		//列出组(页面横向排列的一级组)
			List<Entity> entityList_z = entityService.listAllEntitiesByPId(pd);		//列出此组下架角色
			pd = entityService.findObjectById(pd);							//取得点击的角色组(横排的)
			mv.addObject("pd", pd);
			mv.addObject("entityList", entityList);
			mv.addObject("entityList_z", entityList_z);
			mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
            mv.setViewName(base.concat("/receiptManage/receivables_manage"));
        } catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	/**
	 * 列表总表数据
	 * @param page
	 * @return
	 * @throws Exception
	 */

	@RequestMapping(value="/findAllReceiptInfo")
	@ResponseBody
	public ResponseResult findAllReceiptInfo(int page,int limit) throws Exception{
		PageHelper.startPage(page, limit);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = receiptService.findAllReceiptInfo(pd);

		return ResponseResult.success(new PageInfo(list));
	}
	/**
	 * 列表明显表数据
	 * @param page
	 * @return
	 * @throws Exception
	 */

	@RequestMapping(value="/findReceiptInfo")
	@ResponseBody
	public ResponseResult findReceiptInfo(int page,int limit) throws Exception{
		PageHelper.startPage(page, limit);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = receiptService.findReceiptInfo(pd);

		return ResponseResult.success(new PageInfo(list));
	}
	/**
	 * 复核
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updateReceiptInfo")
	@ResponseBody
	public String updateReceiptInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("id").trim();
			PageData pdq = new PageData();
			pdq.put("id", id);
			pdq.put("is_fin_audi", 1);
			receiptService.updateReceiptInfo(pdq);
		}
		return null;
		
	}
	/**
	 * 冲账处理
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/strikeBalance")
	@ResponseBody
	public String strikeBalance()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.getString("usd");
		pd.getString("rmb");
		if(!pd.getString("usd").isEmpty()){
			pd.put("offset_usd",  Float.parseFloat(pd.getString("usd"))- Float.parseFloat(pd.getString("decuctible_usd")));
		}else if(pd.getString("rmb")!=null&&pd.getString("rmb")!=""){
			pd.put("offset_rmb",  Float.parseFloat(pd.getString("rmb"))- Float.parseFloat(pd.getString("decuctible_rmb")));
		}
		receiptService.updateInfoById(pd);
		return null;
		
	}
	/**
	 * 拆分/预收
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/splitAndAdvance")
	public ModelAndView splitAndAdvance()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName(base.concat("/receiptManage/split_and_advance_list"));
		return view;
		
	}
	
	/**
	 * 收款制单
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/paymentSingle")
	public ModelAndView paymentSingle()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName(base.concat("/receiptManage/receivables_single"));
		return view;
		
	}
	@Override
	public List getData(String type, Map param, Page page) {
		PageData pd = new PageData();

//		pd.put("receipt_app_code", param.get("receipt_app_code"));
		List<Map> pdq= null;
		try {
//			  pdq = receiptService.findReceiptInfo(pd);
			pdq = receiptService.findReviewByCode(param.get("receipt_app_code").toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
        List<Map<String,Object>> map = new ArrayList<Map<String,Object>>(); 
		Map mp = new HashMap<String,Object>();
		String USERNAME = Jurisdiction.getUsername();//当前登录的用户名        	

		mp.put("list", pdq);
//		mp.put("payment_app_dept",param.get("payment_app_dept"));
		mp.put("zhidan", "4848784");
		mp.put("settlement_dept",param.get("settlement_dept"));
		mp.put("SHIPPER_NAME", param.get("SHIPPER_NAME"));
		mp.put("USERNAME", USERNAME);

//		mp.put("settlement_dept",param.get("settlement_dept"));
//		mp.put("payment_app_dept",param.get("payment_app_dept"));
//		mp.put("dept_business_type",param.get("dept_business_type"));
//		mp.put("payment_dept",param.get("payment_dept"));
//		mp.put("due_bank",param.get("due_bank"));
//        mp.put("payment_method",param.get("payment_method"));
//        mp.put("remark",param.get("remark"));
        map.add(mp);
		return map;
	}
	@Override
	public String getService() {
		return  getClass().getSimpleName();
	}
	@Override
	public Map<String, String> setNames() {
		Map m = new HashMap(3);
		//保险申报单
		m.put("Receipt","Receipt.xls");
		m.put("ReceiptName","运费收入缴款单("+ DateUtil.getSdfTimes()+").xls");
		m.put("ReceiptModule",getService());
		return m;
	}

}
